package com.fastloan.mgt.invest.weixin.controller;

import com.fastloan.mgt.invest.model.InvestCusTemp;
import com.fastloan.mgt.invest.service.InvestCusTempService;
import com.fastloan.mgt.invest.weixin.core.WeixinMessage;
import com.fastloan.mgt.invest.weixin.core.WeixinSignUtil;
import com.fastloan.mgt.invest.weixin.model.message.TextMessage;
import com.fastloan.mgt.invest.weixin.service.InitMenuService;
import com.kawins.spring.ContextProperty;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * @author rankai
 *         createTime 2017-08-2017/8/18 10:16
 */
@RestController
@RequestMapping("/api/weixinApp/anon")
public class AppController {

    @Autowired
    private InitMenuService initMenuService;

    @Autowired
    private InvestCusTempService investCusTempService;

    @GetMapping("/test")
    public void test() {
        System.out.println("测试成功");
    }

    @GetMapping("/menu")
    public void initMenu() {
        initMenuService.initMenu();
    }

    @PostMapping(produces = "text/html;charset=UTF-8")
    public String appMain(HttpServletRequest request) {

        WeixinMessage.getUserData(request, Boolean.FALSE);
        String message = "";
        try {
            Map<String, String> map = WeixinMessage.xmlToMap(request);
            String fromUserName = map.get("FromUserName");
            String toUserName = map.get("ToUserName");
            String msgType = map.get("MsgType");

            //非空判断--必须
            String event = map.get("Event") == null ? "" : map.get("Event").toLowerCase();//微信发送过来的是大写
            String content = map.get("Content") == null ? "" : map.get("Content");
            String eventKey = map.get("EventKey") == null ? "" : map.get("EventKey");
            if ("text".equals(msgType)) {
                TextMessage text = new TextMessage();
                text.setFromUserName(toUserName);// 服务器的发送方是微信的接收方
                text.setToUserName(fromUserName);// 服务器的接受方是微信的发送方
                text.setCreateTime(new Date().getTime());
                text.setMsgType("text");
                if (content.substring(0, 2).equals("HB")) {
                    InvestCusTemp investCusTemp = new InvestCusTemp();
                    investCusTemp.setCode(content);
                    InvestCusTemp selectOne = investCusTempService.selectOne(investCusTemp);
                    if (selectOne != null) {
                        selectOne.setCode(1 + "");
                        investCusTempService.updateByPrimaryKeySelective(selectOne);
                        text.setContent("您的问卷红包兑换成功,请注意查收!感谢你的配合.");
                    } else {
                        text.setContent("对不起,该兑换码无效或者已经兑换!感谢你的配合.");
                    }
                } else if (StringUtils.contains(content, "乾康易数") ||
                        StringUtils.contains(content, "易数")) {
                    text.setContent("多角度分析金融业务!");
                } else if (StringUtils.contains(content, "乾康快贷") ||
                        StringUtils.contains(content, "快贷")) {
                    text.setContent("贷款全流程管理!");
                } else if (StringUtils.contains(content, "乾康快信") ||
                        StringUtils.contains(content, "快信")) {
                    text.setContent("查企业，就上快信!");
                } else if (StringUtils.contains(content, "乾康快接") ||
                        StringUtils.contains(content, "快接")) {
                    text.setContent("运营概况全浏览!");
                } else {
                    text.setContent("您发送的消息是:" + content);
                }
                message = WeixinMessage.textMessageToXml(text);
            } else if ("event".equals(msgType) && "subscribe".equals(event)) {  //事件类型----并且是触发关注事件
                TextMessage text = new TextMessage();
                text.setFromUserName(toUserName);// 服务器的发送方是微信的接收方
                text.setToUserName(fromUserName);// 服务器的接受方是微信的发送方
                text.setCreateTime(new Date().getTime());
                text.setMsgType("text");
                text.setContent("欢迎关注乾康易数微信公众号,联系方式: 021-54485390");
                message = WeixinMessage.textMessageToXml(text);
            } else if ("event".equals(msgType) && "click".equals(event)) {
                TextMessage text = new TextMessage();
                text.setFromUserName(toUserName);// 服务器的发送方是微信的接收方
                text.setToUserName(fromUserName);// 服务器的接受方是微信的发送方
                text.setCreateTime(new Date().getTime());
                text.setMsgType("text");
                if (eventKey.equals("1_1")) {
                    text.setContent("https://kuaidai.kuaixin360.com");
                }
                if (eventKey.equals("1_2")) {
                    text.setContent("https://www.kuaixin360.com");
                }
                if (eventKey.equals("1_3")) {
                    text.setContent("https://kuaijie.kuaixin360.com");
                }
                if (eventKey.equals("1_4")) {
                    text.setContent("http://dal.advisingbank.com");
                }
                if (eventKey.equals("2_1")) {
                    text.setContent("我们已经收到你的点赞，感谢你对乾康易数的关注和支持");
                }
                if (eventKey.equals("2_2")) {
                    text.setContent("乾康是农商银行联盟的战略合作伙伴，万事达组织亚太地区战略合作伙伴，某大型电商消费金融的战略合作伙伴。");
                }
                message = WeixinMessage.textMessageToXml(text);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return message;
    }


    /**
     * 此处为微信设置服务器信息使用,设置成功后无用
     *
     * @param request
     * @return
     */
    @GetMapping
    public String init(HttpServletRequest request) {
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        String token = (String) ContextProperty.getContextProperty("weixin.token");
        System.out.println(signature + "," + timestamp + "," + nonce + "," + token);
        if (WeixinSignUtil.checkSignature(token, signature, timestamp, nonce)) {
            return echostr;
        }
        return "";
    }
}
